package com.longcai.fenzhuang.fuyou;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.longcai.entity.payment.fuyou.request.ClyjFuyouAgencyPay;




public class PayforReq {
	private static final String ENCODEING="UTF-8";
	public static   Map<String, String> requestPost(String url,List<NameValuePair> params) throws ClientProtocolException, IOException {
	    CloseableHttpClient httpclient = HttpClientBuilder.create().build();
	         
	    HttpPost httppost = new HttpPost(url);
	        httppost.setEntity(new UrlEncodedFormEntity(params,ENCODEING));
	         
	        CloseableHttpResponse response = httpclient.execute(httppost);
	        System.out.println(response.toString());
	         
	        HttpEntity entity = response.getEntity();
	        System.out.println(entity);
	        String jsonStr = EntityUtils.toString(entity, "utf-8");
	        System.out.println(jsonStr);
	        String xml="<?xml version='1.0' encoding='UTF-8' standalone='yes'?><payforrsp><ret>000000</ret><memo>成功</memo></payforrsp>";
			  Document dom;
			try {
				dom = DocumentHelper.parseText(xml);
				Element root=dom.getRootElement();
				String ret=root.element("ret").getText();
				String memo=root.element("memo").getText();
				System.out.println(ret);
				System.out.println(memo);
				Map<String, String > hashMap = new HashMap<>();
				hashMap.put("ret", ret);
				hashMap.put("memo", memo);
				httppost.releaseConnection();
				return hashMap;
				
			} catch (DocumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
	}
	
	public static String getDate(){
		SimpleDateFormat sf=new SimpleDateFormat("yyyyMMdd");
		Date date=new Date();
		String date1=sf.format(date);
		return date1;		
	}
	
	public AgencyPayResponse agencyPay(ClyjFuyouAgencyPay cfap){
	    try {
	    	String xml = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>"+
	    			"<payforreq>"+
	    			"<ver>1.00</ver>"+
	    			"<merdt>"+PayforReq.getDate()+"</merdt>"+
	    			"<orderno>"+cfap.getOrderno()+"</orderno>"+
	    			"<bankno>"+cfap.getBankno()+"</bankno>"+
	    			"<cityno>"+cfap.getCityno()+"</cityno>"+
	    			"<branchnm>"+cfap.getBranchnm()+"</branchnm>"+
	    			"<accntno>"+cfap.getAccntno()+"</accntno>"+
	    			"<accntnm>"+cfap.getAccntnm()+"</accntnm>"+
	    			"<amt>"+cfap.getAmt()+"</amt>"+
	    			//"<txncd>05</txncd>"+
	    			//"<projectid>0002900F0345178_20160121_0222</projectid>"+
	    			//"<txncd></txncd>"+
	    			//"<projectid></projectid>"+
	    			"</payforreq>";
	    	String macSource = "0002900F0345178|123456|"+"payforreq"+"|"+xml;
	    	String mac = MD5Util.encode(macSource, "UTF-8").toUpperCase();
	        String loginUrl = "https://fht-test.fuiou.com/fuMer/req.do";
	        List<NameValuePair> params = new ArrayList<NameValuePair>();
	        params.add(new BasicNameValuePair("merid", "0002900F0345178"));
	        params.add(new BasicNameValuePair("reqtype", "payforreq"));
	        params.add(new BasicNameValuePair("xml", xml));
	        params.add(new BasicNameValuePair("mac", mac));
	             
	        Map<String, String> hashMap = requestPost(loginUrl,params);
	        String ret = hashMap.get("ret");
	        String memo=hashMap.get("memo");
	        AgencyPayResponse  apr=new AgencyPayResponse();
	        apr.setMemo(memo);
	        apr.setRet(ret);
	        return apr;
	    } catch (ClientProtocolException e) {
	        e.printStackTrace();
	    } catch (IOException e) {
	        e.printStackTrace();
	    } finally {
		}
	    return null;
	}
	
	
}
